var win = Titanium.UI.currentWindow;

var db = Titanium.Database.open('dagboksDB');

var data = [];

var rows = db.execute('SELECT * FROM dagbok WHERE CHILDID = ?',win.childId);

while (rows.isValidRow()){
    var row = Ti.UI.createTableViewRow({height:'auto', idname:rows.fieldByName('ID')});
    row.add(Ti.UI.createLabel({text:rows.fieldByName('RUBRIK'), left:10, top:3, height:'auto', width:300, font:{fontSize:18, fontFamily:'Marker Felt', fontWeight:'bold'}}));
	row.add(Ti.UI.createLabel({text:rows.fieldByName('POSTDATE'), left:10, top:26, height:'auto', width:300, font:{fontSize:9}}));
    row.add(Ti.UI.createLabel({text:rows.fieldByName('POST'), left:10, top:40, height:'auto', width:300}));
    data.push(row);
    rows.next();
}
rows.close();

var tableview = Titanium.UI.createTableView({
	data:data, 
	rowHeight:'auto', 
	width:'auto',
	left:3,
	right:3,
	top:5,
	height:'auto',
	/*borderWidth:1,
	borderRadius:5,
	borderColor:"#ccc",*/
	backgroundColor:"black",
	minRowHeight:80,
});

//listener f?r sj?lva table. klick h?r = visa alertDialogen
tableview.addEventListener('click', function(e){
	idname = e.rowData.idname;
	
	if (e.rowData.idname){  
		alertDialog1.show();
	}	
});

var alertDialog1 = Titanium.UI.createAlertDialog({
    title: 'Sure?',
    message: 'Are you really sure you want to remove this post?',
    buttonNames: ['Yes','No']
});
  
var idname = null;

// notification pop-up
var notice = Ti.UI.createNotification({message:"Post deleted."});
// Set the duration to either Ti.UI.NOTIFICATION_DURATION_LONG or NOTIFICATION_DURATION_SHORT
notice.duration = Ti.UI.NOTIFICATION_DURATION_LONG;

// Setup the X & Y Offsets
notice.offsetX = 0;
notice.offsetY = 20;

//listener f?r alertDialogen  
alertDialog1.addEventListener('click', function(e){
	if (e.index == 0){
		db.execute('DELETE FROM dagbok WHERE ID = ?', idname)
		
		var win2 = Titanium.UI.createWindow({
			url:'viewchild.js'
		});
		notice.show()
	}else if(e.index == 1){
		var win2 = Titanium.UI.createWindow({
			url:'viewchild.js'
		});
	}
	win2.open({modal:true});  
});

win.add(tableview);

var menu = Ti.UI.Android.OptionMenu.createMenu();

Ti.include("menu.js");